package yxy.game.pm2.command.jadeseal;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.medalrefine;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.JadeSealMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.jadeseal.JadeSeal;
import yxy.game.pm2.module.jadeseal.JadeSealModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

import java.util.Collection;

/** 玉玺 阵营玉玺升阶 */
@MessageType(proto = Command.JadeSealStageUp, classtype = JadeSealMessage.JadeSealStageUpRequest.class)
public class CMD_JadeSealStageUp extends Command<JadeSealMessage.JadeSealStageUpRequest> {

	@Override
	public void execute(MessageSource messageSource, JadeSealMessage.JadeSealStageUpRequest request) {
		String playerCode = messageSource.getTag();
		int camp = request.getCamp();
		JadeSealMessage.JadeSealStageUpResponse.Builder response = JadeSealMessage.JadeSealStageUpResponse.newBuilder();
		response.setCamp(camp);
		
		JadeSealModule jsmodule = JadeSealModule.get(playerCode);
		if(jsmodule == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		JadeSeal js = jsmodule.getJss().get(camp);
		if(js == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(!js.isOpen()) {
			send(messageSource, response, Result.JadeSealNeedUnlock);
			return;
		}
		
		medalrefine mr = DataCenter.getData(js.countmrid(), medalrefine.class);
		if(mr == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		//玉玺等级判断
		if(js.getLevel() < mr.getCondition()) {
			send(messageSource, response, Result.JadeSealNeedLevelUp);
			return;
		}
		
		int nextstageid = js.countnextmrid();
		medalrefine nextmr = DataCenter.getData(nextstageid, medalrefine.class);
		if(nextmr == null) {
			send(messageSource, response, Result.JadeSealStageUpper);
			return;
		}
		
		Collection<Currency> costcurs = Currency.create(mr.getCosttype(), mr.getCostid(), mr.getCostnum());
		try {
			new CurrencyHandle().cost(playerCode, CCReason.玉玺, costcurs);
		} catch (CurrencyNotEnoughException e) {
			send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
			return;
		}
		
		if(nextmr.getLevel() > js.getStagelevel()) {
			js.setStagelevel(nextmr.getLevel());
		}
		js.setStagestage(nextmr.getStage());
		jsmodule.set();

		PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
		PlayTypeHandle.saveJadeSeal(playerCode,playTypeModule);//提前把战力算到中间缓存

		new BattleHandle().powerUpdate(playerCode);// 战力更新
		
		response.setStagelevel(js.getStagelevel());
		response.setStagestage(js.getStagestage());
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, JadeSealMessage.JadeSealStageUpResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}